package com.sjz.chat2_1;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText content;
    private Button yiban;
    private Button btn_main_start2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content=findViewById(R.id.content);
        yiban=findViewById(R.id.yiban);
        btn_main_start2=findViewById(R.id.btn_main_start2);
        //设置监听
        yiban.setOnClickListener(this);
        btn_main_start2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {//v就是发生事件的视图对象(操作的)
       if(v==yiban){  //一般启动
           Intent intent=new Intent(this,SecondActivity.class);
           String message=content.getText().toString();
           intent.putExtra("message",message);  //设置参数
           startActivity(intent);   //3). 启动Activity
       }else if(v==btn_main_start2){   //带返回值的activity
           Intent intent=new Intent(this,SecondActivity.class);
           String message=content.getText().toString();
           intent.putExtra("message",message);
           int requestCode=2;
           startActivityForResult(intent,requestCode);
       }
    }

    /***
     * 带返回值需要重写的方法
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==2 && resultCode==3){
            String result=data.getStringExtra("RESULT");
            content.setText(result);
        }

    }
}